//
//  MainViewController.m
//  VietQuan
//
//  Created by hainguyen on 11/13/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import "ItemMainViewController.h"
#import "SettingMainViewController.h"
#import "AreaMainViewController.h"
#import "RestaurantMainViewController.h"

#define kFoodTabBar 1
#define kResTabBar 2
#define kAreaTabBar 3
#define kOptionTabBar 4

@implementation MainViewController
@synthesize navigationController;
@synthesize tabBarView;
@synthesize tabBarBG;
@synthesize tabbarFoodButton;
@synthesize tabbarResButton;
@synthesize tabbarAreaButton;
@synthesize tabbarSettingButton;
@synthesize itemMainController;
@synthesize areaMainController;
@synthesize resMainController;
@synthesize settingMainController;

- (IBAction)tabbarButtonPressed:(id)sender{
    UIButton *button = (UIButton*)sender;
    switch (button.tag) {
        case kFoodTabBar:
            [tabbarFoodButton setSelected:YES];
            [tabbarResButton setSelected:NO];
            [tabbarAreaButton setSelected:NO];
            [tabbarSettingButton setSelected:NO];
            [self.navigationController popToRootViewControllerAnimated:YES];
            break;
        case kResTabBar:
            [tabbarFoodButton setSelected:NO];
            [tabbarResButton setSelected:YES];
            [tabbarAreaButton setSelected:NO];
            [tabbarSettingButton setSelected:NO];
            if (resMainController == nil) {
                resMainController = [[RestaurantMainViewController alloc] init];
            }
            [self.navigationController popToRootViewControllerAnimated:NO];
            [self.navigationController pushViewController:resMainController animated:YES];
            break;
        case kAreaTabBar:
            [tabbarFoodButton setSelected:NO];
            [tabbarResButton setSelected:NO];
            [tabbarAreaButton setSelected:YES];
            [tabbarSettingButton setSelected:NO];
            if (areaMainController == nil) {
                areaMainController = [[AreaMainViewController alloc] init];
            }
            [self.navigationController popToRootViewControllerAnimated:NO];
            [self.navigationController pushViewController:areaMainController animated:YES];
            break;
        case kOptionTabBar:
            [tabbarFoodButton setSelected:NO];
            [tabbarResButton setSelected:NO];
            [tabbarAreaButton setSelected:NO];
            [tabbarSettingButton setSelected:YES];
            if (settingMainController == nil) {
                settingMainController = [[SettingMainViewController alloc] init];
            }
            [self.navigationController popToRootViewControllerAnimated:NO];
            [self.navigationController pushViewController:settingMainController animated:YES];
            break;
    }
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view addSubview:navigationController.view];
    [navigationController.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:tabBarView];
    [tabBarView setFrame:CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44)];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc{
    [navigationController release];
    [tabBarView release];
    [tabBarBG release];
    [tabbarFoodButton release];
    [tabbarResButton release];
    [tabbarAreaButton release];
    [tabbarSettingButton release];
    [itemMainController release];
    [areaMainController release];
    [resMainController release];
    [settingMainController release];
    [super dealloc];
}

@end
